#include "game.h"

//左边，右边，当前
Obj*left_list;
Obj*right_list;
Obj*cur_list;
//列表标题
//路径data[0]
Obj*list_tile;
//记录初始按下位置
Obj*touch;
//文件，文件夹
Spr*file_spr;
Spr*folder_spr;
//返回，复制，同步，删除
Obj*back_obj;
Obj*copy_obj;
Obj*swap_obj;
Obj*delete_obj;

//绘制路径obj，路径data[0]
//data[1]宽度，data[2]高度
//数目data[3]
void drawPath(void*ob)
{
 char data[100];
 Obj*obj = ob;
 //获取
 int w = maxInt(obj->data[1],100);
 int h = maxInt(obj->data[2],40);
 char*path = (char*)obj->data[0];
 //clipRect(obj->x,obj->y,w,h);
 
 //绘制背景
 drawRect(obj->x,obj->y,
  SW-obj->x,h,0x80ffffff);
 drawRect(obj->x+1,obj->y+1,
  SW-obj->x-2,h-2,0xff65aef0);
 if(obj->data[0]!=-1)
 {
  //截取路径
  int textw = 
  fnt_getLineW(GM.fnt,path);
  fnt_drawline(GM.fnt,path,
   obj->x-(textw-w)-5,obj->y+5);
  
  sprintf(data,"num:   %d",obj->data[3]);
  textw = 
  fnt_getLineW(GM.fnt_small,data);
  fnt_drawline(GM.fnt_small,data,
   obj->x-(textw-w)-5,obj->y+45);
   
 }
 //clipRect(0,0,SW,SH);
}

//绘制文件列表，路径data[0]
//data[1]总宽度，data[2]单元高度
//data[3]总高度，data[4]y偏移
//data[5]总项目，data[6]选中项目
//data[7]选中name
void drawFileList(void*ob)
{
 int tmp = 0;
 Obj*obj = ob;
 if(obj->data[0]==-1)
 	return;
 
 //路径
 char name[100];
 char data[500];
 int w = maxInt(obj->data[1],100);
 int h = maxInt(obj->data[2],40);
 int mh = maxInt(obj->data[3],40);
 int dy = obj->data[4];
 int se = obj->data[6];
 char*path = (char*)obj->data[0];
 
 //clipRect(obj->x,obj->y,w,mh);
 drawRect(obj->x,obj->y,w,mh,
  0x80000000);
 //遍历文件列表
 int32 find = 
 findstart(path,name,100);
 int isEnd = find;
 int count = 0;
 while(isEnd!=-1)
 {
  if(strcmp(name,"..")!=0)
  	if(strcmp(name,".")!=0)
  {
   if(obj->y+dy+(count+1)*h>=0)
   	if(obj->y+dy+count*h<=SH)
   {
    //绘制背景
    drawRect(obj->x,obj->y+dy+
     count*h,
     SW-obj->x,h,0xfff0f0f0);
    
    //选中
    if(se!=count)
    	drawRect(obj->x+1,obj->y+1+dy+
     count*h,
     SW-obj->x-2,h-2,0xffffffff);
    else
    {
     obj->data[7] = 
     getRes(name,tStr)->id;
     drawRect(obj->x+1,obj->y+1+dy+
      count*h,
      SW-obj->x-2,h-2,0xfff0f0f0);
    }
    
    memset(data,'\0',500);
    sprintf(data,"%s/%s",path,name);
    //绘制
    fnt_drawline(GM.fnt,
     name,obj->x+10+h,
     obj->y+dy+10+count*h);
    //绘制文件/文件夹
    if(filestate(data)==IS_FILE)
    {
     drawSpr(file_spr,obj->x+10,
      obj->y+dy+15+count*h);
     tmp = getlen(data);
     memset(data,'\0',500);
     sprintf(data,"%dB",tmp);
     //文件大小
     fnt_drawline(GM.fnt_small,
      data,obj->x+10+h,
      obj->y+dy+count*h+50);
    }
    else
    {
     drawSpr(folder_spr,obj->x+10,
      obj->y+dy+15+count*h);
    }
   }
   count++;
  }
  isEnd = findnext(find,name,100);
 }
 obj->data[5] = count;
 findstop(find);
 //clipRect(0,0,SW,SH);
}

//打开文件
void openFile(char*path)
{
 char data[300];
 char*code = "{\"type\":\"*/*\",\"data\": \"sdcard/%s\",\"action\":\"android.intent.action.VIEW\"}";
 
 sprintf(data,code,path);
 char*retext = 
 ex_call("startActivity",data);
 if(retext!=NULL)
 {
  //printf(data);
  //printf(retext);
  free(retext);
 }
}

//刷新列表
//data[1]总宽度，data[2]单元高度
//data[3]总高度，data[4]y偏移
//data[5]总项目，data[6]选中项目
//data[7]选中name
void refList(Obj*obj,char*path)
{
 if(path!=NULL)
 {
  if(filestate(path)==IS_DIR)
  {
   obj->data[0] = (int32)path;
   obj->data[4] = 0;
   obj->data[6] = -1;
  }
  else
  	openFile(path);
 }
}

//房间创建
void homeCreate(void*rm)
{
 Obj*obj;
 Spr*spr;
 Room*room = rm;
 //载入资源
 GM.fnt = (_FNT*)
 getRes("fnt/font.fnt",tFnt)->id;
 GM.fnt_small = (_FNT*)
 getRes("fnt/small.fnt",tFnt)->id;
 spr = (Spr*)
 getRes("spr/file.spr",tSpr)->id;
 file_spr = spr;
 spr = (Spr*)
 getRes("spr/folder.spr",tSpr)->id;
 folder_spr = spr;
 
 spr = (Spr*)
 getRes("spr/back.spr",tSpr)->id;
 obj = newObj(SW-100,SH-40,spr);
 obj->data[0] = 4;
 back_obj = obj;
 roomAddObj(room,obj);
 
 spr = (Spr*)
 getRes("spr/copy.spr",tSpr)->id;
 obj = newObj(100,SH-40,spr);
 obj->data[0] = 1;
 copy_obj = obj;
 roomAddObj(room,obj);
 
 spr = (Spr*)
 getRes("spr/delete.spr",tSpr)->id;
 obj = newObj(250,SH-40,spr);
 obj->data[0] = 2;
 delete_obj = obj;
 roomAddObj(room,obj);
 
 spr = (Spr*)
 getRes("spr/swap.spr",tSpr)->id;
 obj = newObj(SW-250,SH-40,spr);
 obj->data[0] = 3;
 swap_obj = obj;
 roomAddObj(room,obj);
 
 //文件列表
 obj = newObj(0,80,NULL);
 obj->data[1] = SW/2-1;
 obj->data[2] = 80;
 obj->data[3] = SH-160;
 obj->data[4] = 0;
 obj->data[5] = 0;
 obj->data[6] = -1;
 obj->draw = drawFileList;
 left_list = obj;
 roomAddObj(room,obj);
 
 obj = newObj(SW/2,80,NULL);
 obj->data[1] = SW/2;
 obj->data[2] = 80;
 obj->data[3] = SH-160;
 obj->data[4] = 0;
 obj->data[5] = 0;
 obj->data[6] = -1;
 obj->draw = drawFileList;
 right_list = obj;
 roomAddObj(room,obj);
 
 obj = newObj(0,0,NULL);
 list_tile = obj;
 obj->data[0] = (int32)"";
 obj->data[1] = SW;
 obj->data[2] = 80;
 obj->draw = drawPath;
 roomAddObj(room,obj);
 
 obj = newObj(0,0,NULL);
 touch = obj;
 roomAddObj(room,obj);
 
 refList(left_list,"sdcard");
 refList(right_list,"sdcard");
 cur_list = left_list;
}

//绘制房间
void homeDraw(void*rm)
{
 Room*room = rm;
 list_tile->data[0] = 
 cur_list->data[0];
 list_tile->data[3] = 
 cur_list->data[5];
 
 //绘制对象
 drawSenObjCam(room->sen,
  &(room->cam));
 
 //绘制菜单
 drawRect(0,SH-80,
  SW,100,0x80f0f0f0);
 
 //绘制蒙版
 drawRect(cur_list->x,cur_list->y,
  cur_list->data[1],
  cur_list->data[3],0x10000000);
 
}

void listEvent(Obj*list,int type,int p1,int p2)
{
 int dd = -1,dy;
 char*temp;
 char data[500];
 //按下
 switch(type)
 {
  case MS_DOWN:
  {
   touch->y = p2;
   touch->y = p2;
   
   //计算选中
   dy = p2-list->y-list->data[4];
   dd = dy/list->data[2];
   dd = maxInt(dd,0);
   //下一级
   if(list->data[6]==dd)
   {
    temp = (char*)list->data[0];
    if(strcmp(temp,"")==0)
    	sprintf(data,"%s",
     list->data[7]);
    else
    	sprintf(data,"%s/%s",
     temp,list->data[7]);
    temp = (char*)
    getRes(data,tStr)->id;
    refList(list,temp);
   }
   //选中
   else if(dd<list->data[5])
   {
    list->data[6] = dd;
   }
   else
   	list->data[6] = -1;
  }
  break;
  
  //归位
  case MS_UP:
  {
   list->data[4]
    = minInt(list->data[4],0);
  }
  break;
  
  //拖拽
  case MS_MOVE:
  {
   list->data[4]-=touch->y-p2;
   touch->y = p2;
  }
  break;
  
  //退出页面
  case KY_UP:
  {
   if(p1==_BACK)
   {
    temp = (char*)list->data[0];
    if(temp==NULL
      ||strcmp(temp,"")==0
      ||strcmp(temp,"/")==0)
    {
     exitRoom(_room);
     exit();
    }
    else
    {
     char*pos = strrchr(temp,'/');
     if(pos==NULL)
     	refList(list,"");
     else
     {
      memset(data,'\0',500);
      dd = strlen(temp)-strlen(pos);
      strncpy(data,temp,dd);
      refList(list,(char*)
       getRes(data,tStr)->id);
     }
    }
    
   }
  }
  break;
 }
}

//房间事件
void homeEvent(int type,int p1,int p2)
{
 int dd = -1;
 Obj*obj;
 //存储当前列表
 Obj*list;
 
 if(MS_DOWN==type)
 {
  touch->y = p2;
 }
 
 if(MS_DOWN==type
   ||MS_MOVE==type
   ||MS_UP==type)
 {
  dd = isPointCollRect(p1,p2,
   left_list->x,left_list->y,
   left_list->data[1],
   left_list->data[3]);
  if(dd)
  {
   right_list->data[6] = -1;
   right_list->data[4] = 
   minInt(right_list->data[4],0);
   cur_list = left_list;
  }
  dd = isPointCollRect(p1,p2,
   right_list->x,right_list->y,
   right_list->data[1],
   right_list->data[3]);
  if(dd)
  {
   left_list->data[6] = -1;
   left_list->data[4] = 
   minInt(left_list->data[4],0);
   cur_list = right_list;
  }
 }
 
 //按钮
 //拦截下面的事件
 if(MS_DOWN==type)
 {
  obj = 
  pointSenObj(_room->sen,p1,p2);
  if(obj!=NULL)
  {
   shake(10);
   switch(obj->data[0])
   {
    case 4:
    homeEvent(KY_UP,_BACK,0);
    break;
    case 3:
    if(cur_list==left_list)
    {
     refList(right_list,
      (char*)left_list->data[0]);
     right_list->data[4] = 
     left_list->data[4];
    }
    else
    {
     refList(left_list,
      (char*)right_list->data[0]);
     left_list->data[4] = 
     right_list->data[4];
    }
    break;
   }
   return;
  }
 }
 
 list = cur_list;
 //列表事件
 if(list!=NULL)
 {
  listEvent(list,type,p1,p2);
 }
}

//跳转房间
int gotoHome()
{
 if(GM.rmHome==NULL)
 {
  GM.rmHome = 
  newRoom(SW,SH,100);
 }
 switchPath("d");
 GM.rmHome->create = homeCreate;
 GM.rmHome->draw = homeDraw;
 GM.rmHome->event = homeEvent;
 gotoRoom(GM.rmHome);
 return 0;
}




